<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/static/common/inner-header.jspf"%>
<link rel="stylesheet" href="<%=request.getContextPath() %>/static/common/plugin/nice-validator-0.10.5/dist/jquery.validator.css" />
<script src="<%=request.getContextPath() %>/static/common/plugin/nice-validator-0.10.5/dist/jquery.validator.js"></script>
<script src="<%=request.getContextPath() %>/static/common/plugin/nice-validator-0.10.5/dist/local/zh-CN.js"></script>
<script src="<%=request.getContextPath() %>/static/sys/user/js/user-add.js"></script>
<title>添加用户</title>
</head>
<body bgcolor="#ADD8E6">
	<div style="margin-left: 30%; margin-top: 10%; font-size: 15px;">
		<form id="myform" action="<%=request.getContextPath() %>/user/AllUserCommandServlet?command=addUser" method="post" >
			<b>帐　　号:</b><input type="text" class="code" name="code" value="" placeholder="请输入账号……" id="addCode"/>
			<span id="codeInfo" class="errorInfo"></span><br><br>
			<b>姓　　名:</b><input class="name" name="name" type="text" value="" placeholder="请填写姓名……" id="addName"/>
			<span id="nameInfo"  class="errorInfo"></span><br><br>
			<b>性　　别:</b><input name="sex" type="radio" checked="checked" value="1"/>&nbsp;男&nbsp;&nbsp;&nbsp;<input name="sex" type="radio" value="2"/>&nbsp;女<br><br>
			<b>所属角色:</b><select class="addRoleName" name="addRoleName"><option value="">--请选择--</option>
				<script>
					var roleList=eval(<%=request.getAttribute("roleList")%>);
					for(var i=0; i < roleList.length; i++){
						document.write('<option value="'+roleList[i]+'">' + roleList[i] + '</option>');
					}
				</script>
			</select><span id="roleInfo"  class="errorInfo"></span><br><br>
			<b>电　　话:</b><input class="phone" name="phone" type="text" value="" id="addphone"/>
			<span id="phoneInfo"  class="errorInfo"></span><br><br>
			<b>邮　　箱:</b><input class="email" name="email" type="text" value="" id="addEmail"/>
			<span id="emailInfo"  class="errorInfo"></span><br><br>
			<div id="hideClass"><b>所属班级:</b><select class="addClassName" name="addClassName"><option value="">--请选择--</option>
				<script>
					var classList=eval(<%=request.getAttribute("classList")%>);
					for(var i=0; i < classList.length; i++){
						document.write('<option value="'+classList[i]+'">' + classList[i] + '</option>');
					}
				</script>
			</select><span id="classInfo"  class="errorInfo"></span><br><br></div>
			<b style="display:block; margin-top:0;float:left;">备　　注：</b><textarea name="addMemo" style="resize:none; width:240px; height:80px;"/></textarea><br><br>
			&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="添加"/>&nbsp;&nbsp;<input type="reset" value="重置"/>
		</form>
		</div>
</body>
<script>
$(function(){
	/**
	 * 表单提交时阻止提交进行验证
	 */
	 $("#myform").submit(function(){
		 return validata();
	 });
	/**
	 * 重置是清空info
	 */
	 $(":reset").click(function(){
		$(".errorInfo").html(""); 
	 });
	/**
	* 账号输入提示
	*/
	$(".code").focus(function(){
		$("#codeInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/loading.gif"/>账号请用小写字母或数字或下划线或减号，长度为3-16');
	});
	/**
	* 账号验证
	*/
	$(".code").blur(function(){
		var code=$(".code").val();
		 if(code==""){   
			 $("#codeInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>账号不能为空！');
		 }else{
			   reg=/^[a-z0-9_-]{3,16}$/;   
		        if(!reg.test(code)){        
		        	 $("#codeInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>账号格式不对！');
		        }else{   
					 var xmlhttp;
						if (window.XMLHttpRequest)
						  {// code for IE7+, Firefox, Chrome, Opera, Safari
						  xmlhttp=new XMLHttpRequest();
						  }
						else
						  {// code for IE6, IE5
						  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
						  }
						xmlhttp.onreadystatechange=function()
						  {
							if (xmlhttp.readyState==4 && xmlhttp.status==200)
						    {
								var flag=xmlhttp.responseText;
								if(flag==1){
									$("#codeInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/ok.gif"/>账号可以使用！');
								}else{
									$("#codeInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>用户已存在！');
								}
						 	}
						  }
						xmlhttp.open("GET",'<%=request.getContextPath()%>/user/AllUserCommandServlet?command=checkCode&code='+code,true);
						xmlhttp.send();
				 }
			 }
	});
	/**
	 * 验证姓名
	 */
	$(".name").focus(function(){
		$("#nameInfo").html("");
	});
	$(".name").blur(function(){
		if($(".name").val()==""){  
        	$("#nameInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>姓名不能为空');
        }
	});
	/**
	 * 角色验证
	 */
	 $(".addRoleName").change(function(){
		 if($(".addRoleName").val()!=""){
			 $("#roleInfo").html("");
		 }else{
			 $("#roleInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>请选择角色');
		 }
	 });
	/**
	 * 电话验证唯一性
	 */
	 $(".phone").focus(function(){
		 $("#phoneInfo").html("");
	 });
	$(".phone").blur(function(){
		var phone=$(".phone").val();
		 if(phone==""){   
			 $("#phoneInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>电话不能为空！');
		 }else{
			   reg1=/^13[\d]{9,9}$/;   
			   reg2=/^15[0|1|2|3|9][\d]{8}$/;  
			   reg3=/^18[0|1|2|7|9][\d]{8}$/;  
		        if(!reg1.test(phone)&&!reg2.test(phone)&&!reg3.test(phone)){        
		        	 $("#phoneInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>电话号码不对！');
		        }else{   
					 var xmlhttp;
						if (window.XMLHttpRequest)
						  {// code for IE7+, Firefox, Chrome, Opera, Safari
						  xmlhttp=new XMLHttpRequest();
						  }
						else
						  {// code for IE6, IE5
						  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
						  }
						xmlhttp.onreadystatechange=function()
						  {
							if (xmlhttp.readyState==4 && xmlhttp.status==200)
						    {
								var flag=xmlhttp.responseText;
								if(flag==1){
									$("#phoneInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/ok.gif"/>该号码可以使用！');
								}else{
									$("#phoneInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>该号码已被使用！');
								}
						 	}
						  }
						xmlhttp.open("GET",'<%=request.getContextPath()%>/user/AllUserCommandServlet?command=checkPhone&phone='+phone,true);
						xmlhttp.send();
				 }
			 }
	});
	/**
	 * 邮箱验证唯一性
	 */
	 $(".email").focus(function(){
		 $("#emailInfo").html("");
	 });
	 $(".email").blur(function(){
			var email=$(".email").val();
			 if(email==""){   
				 $("#emailInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>邮箱不能为空！');
			 }else{
				    reg1=/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
					reg2=/^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/
			        if(!reg1.test(email)&&!reg2.test(email)){        
			        	 $("#emailInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>邮箱格式不对！');
			        }else{   
						 var xmlhttp;
							if (window.XMLHttpRequest)
							  {// code for IE7+, Firefox, Chrome, Opera, Safari
							  xmlhttp=new XMLHttpRequest();
							  }
							else
							  {// code for IE6, IE5
							  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
							  }
							xmlhttp.onreadystatechange=function()
							  {
								if (xmlhttp.readyState==4 && xmlhttp.status==200)
							    {
									var flag=xmlhttp.responseText;
									if(flag==1){
										$("#emailInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/ok.gif"/>该邮箱可以使用！');
									}else{
										$("#emailInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>该邮箱已被注册！');
									}
							 	}
							  }
							xmlhttp.open("GET",'<%=request.getContextPath()%>/user/AllUserCommandServlet?command=checkEmail&email='+email,true);
							xmlhttp.send();
					 }
				 }
	 });
	 $(".addClassName").change(function(){
		 if($(".addClassName").val()==""){
			 $("#classInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>请选择班级！');
		 }else{
			 $("#classInfo").html("");
		 }
	 });
});
/**
* 提交时表单验证
*/
 function validata(){   
		if($('.code').val()==""){
			$('#codeInfo').html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>请输入账号');
			return false
		}
        if($(".name").val()==""){  
        	$("#nameInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>请输入姓名');
            return false;   
        }
        if($(".addRoleName").val()==""){
        	$("#roleInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>请选择角色');
        	return false;
        }
        if($(".phone").val()==""){   
        	$("#phoneInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>电话不能为空');
        	return false;
        }   
        if($(".email").val()==""){   
        	$("#emailInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>邮箱不能为空');
        	return false;
        }
        if(!$("#hideClass").is(":hidden")){
        	if($(".addClassName").val()==""){
        		$("#classInfo").html('<img style="width:15px;height:15px;" src="<%=request.getContextPath()%>/static/common/images/null.gif"/>请选择班级！');
        		return false;
        	}
        }
        return true;
    }   
</script>
</html>
